"""
@File    :   request_validation.py
@Time    :   2024/02/22 17:57:35
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   请求参数验证的异常处理
"""

from fastapi import Request
from fastapi.exceptions import RequestValidationError

from src.enums.http_response_code import HttpResponseEnum
from src.typings import RequestValidationErrorConvertedDataT
from src.utils.request_validation_data_convert import (
    FastapiPydanticValidateErrorDataUtil,
    PydanticValidateErrorMessagesDict,
)
from src.utils.unified_response import FailApiJSONResponse


async def validation_exception_handler(request: Request, exc: RequestValidationError):
    """处理请求参数验证的异常"""
    # 对错误信息进行转换, 最后将其写入data
    err_data: RequestValidationErrorConvertedDataT = (
        FastapiPydanticValidateErrorDataUtil.data_convert(
            exc, PydanticValidateErrorMessagesDict
        )
    )
    return FailApiJSONResponse(
        status_code=HttpResponseEnum.SUCCESS.value.code,
        code=HttpResponseEnum.PARAMS_VALID_ERROR.value.code,
        message=HttpResponseEnum.PARAMS_VALID_ERROR.value.msg,
        data=err_data,
    )
